/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cq.bd.dao.jdbc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.cq.base.domain.Page;
import com.cq.base.orm.jdbc.BaseJdbcDao;
import com.cq.bd.domain.SStaff;
import com.cq.bd.utils.StringUtil;

/**
 * 员工
 * 
 * @author huangmy
 * @date   2009-10-7
 */
public class StaffJdbcDao extends BaseJdbcDao {
    /**
     * 员工
     * 
     * @param filter
     * @param like
     * @param sort
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page pageStaffs(Map<String, String> filter, Map<String, String> like, Map<String, String> sort, int pageNo,
            int pageSize) {
        String regionId = filter.get("regionId"); // 组织ID
        String departId = filter.get("departId"); // 部门ID
        String countSql = null;
        String querySql = null;
        List<Object> params = new ArrayList<Object>();
        
        if (StringUtil.hasText(regionId) && !StringUtil.hasText(departId)) {
            // 只有组织ID
            countSql = "select count(*) from s_staff where region_id=?";
            querySql = "select * from s_staff where region_id=? limit " + pageSize;
            params.add(new Long(regionId));
        } else if (StringUtil.hasText(regionId) && StringUtil.hasText(departId)) {
            // 有组织ID和部门ID
            countSql = "select count(*) from s_staff where depart_id=?";
            querySql = "select * from s_staff where depart_id=? limit " + pageSize;
            params.add(new Long(departId));
        } else {
            // 两者都无
            countSql = "select count(*) from s_staff";
            querySql = "select * from s_staff limit " + pageSize;
        }
                
        int sum = getSimpleJdbcTemplate().queryForInt(countSql, params.toArray());
        Page page = getBaseJdbcTemplate().pagedQuery(querySql, pageNo, pageSize, sum, SStaff.class, params.toArray());
        return page;
    }
}
